/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.damc.admin.biz.mapper.ClassScriptMapper;
import cn.icanci.loopstack.damc.admin.biz.service.ClassScriptService;
import cn.icanci.loopstack.damc.admin.common.model.base.ClassScriptVO;
import cn.icanci.loopstack.damc.admin.dal.common.PageList;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.ClassScriptDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.ClassScriptDO;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/16 22:44
 */
@Service("classScriptService")
public class ClassScriptServiceImpl implements ClassScriptService {
    @Resource
    private ClassScriptDAO    classScriptDAO;
    @Resource
    private ClassScriptMapper scriptMapper;

    /**
     * 查询所有 T
     *
     * @return 返回所有T
     */
    @Override
    public List<ClassScriptVO> queryAll() {
        return scriptMapper.dos2vos(classScriptDAO.queryAll());
    }

    /**
     * 保存或者更新一条信息
     *
     * @param classScriptVO t
     */
    @Override
    public void save(ClassScriptVO classScriptVO) {
        ClassScriptDO hasOne = classScriptDAO.queryUniqueOne(scriptMapper.vo2do(classScriptVO));
        if (hasOne != null && StringUtils.isBlank(classScriptVO.getId())) {
            throw new IllegalStateException("当前需要被Mock的方法已经存在，请检查");
        }
        if (doInsert(classScriptVO)) {
            ClassScriptDO insert = scriptMapper.vo2do(classScriptVO);
            classScriptDAO.insert(insert);
        } else {
            classScriptDAO.update(scriptMapper.vo2do(classScriptVO));
        }
    }

    /**
     * 根据id进行查询
     *
     * @param id id
     * @return 返回查询的结果
     */
    @Override
    public ClassScriptVO queryById(String id) {
        return scriptMapper.do2vo(classScriptDAO.queryOneById(id));
    }

    /**
     * 分页查询
     *
     * @param classScriptVO T
     * @param pageNum       pageNum
     * @param pageSize      pageSize
     * @return 返回分页查询结果
     */
    @Override
    public PageList<ClassScriptVO> queryPage(ClassScriptVO classScriptVO, int pageNum, int pageSize) {
        PageList<ClassScriptDO> pageQuery = classScriptDAO.pageQuery(scriptMapper.vo2do(classScriptVO), pageNum, pageSize);
        return new PageList<>(scriptMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());
    }

    /**
     * 根据id删除配置[物理删除]
     *
     * @param id id
     */
    @Override
    public void deleteById(String id) {
        classScriptDAO.deleteById(id);
    }

    @Override
    public List<ClassScriptVO> queryEnableByAppCodeAndClazzName(String appCode, String clazzName) {
        return scriptMapper.dos2vos(classScriptDAO.queryEnableByAppCodeAndClazzName(appCode, clazzName));
    }

    @Override
    public List<ClassScriptVO> queryEnableByAppCode(String appCode) {
        return scriptMapper.dos2vos(classScriptDAO.queryEnableByAppCode(appCode));
    }
}
